﻿using System.IO;
using System.Runtime.Serialization.Json;
using LEShortcut.Models;

namespace LEShortcut.Helper
{
    internal static class ConfigHelper
    {
        /// <summary>
        ///     Save config file
        /// </summary>
        /// <param name="config"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        internal static bool SaveConfig(AppConfig config, string path)
        {
            try
            {
                var js = new DataContractJsonSerializer(typeof (AppConfig));
                var fs = new FileStream(path, FileMode.OpenOrCreate);
                js.WriteObject(fs, config);
                fs.Dispose();
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        ///     Load config file
        /// </summary>
        /// <param name="config"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        // ReSharper disable once RedundantAssignment
        internal static bool LoadConfig(ref AppConfig config, string path)
        {
            try
            {
                var js = new DataContractJsonSerializer(typeof (AppConfig));
                var fs = new FileStream(path, FileMode.Open);
                config = (AppConfig) js.ReadObject(fs);
                fs.Dispose();
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}